<?php
class Template {
	private $vars;

	function __construct($file, $string = false) {
		$this->log = new Logger("Template", _LOGPATH_);
		$this->log->setLogLevel(Logger::VERBOSE);

		if($string){
			$this->file = $file;
		}else if (!file_exists("tpl/".$file.".tpl.php")){
			$this->file = "tpl/404.tpl.php";
			$this->log->info("File 'tpl/".$file.".tpl.php' not found");
		}else{
			$this->file = "tpl/".$file.".tpl.php";
		}
	}

	function set($name, $value) {
		$this->vars[$name] = is_object($value) ? $value->fetch() : $value;
	}

	function fetch($file = null) {
		if(!$file) $file = $this->file;
		if (is_array($this->vars)) extract($this->vars);
		ob_start();
		if(is_file($file)){
			$this->log->verbose("include $file");
			include($file);
		}else{
			$this->log->verbose("print '".substr($file, 0, 10)."...'");
			//eval ($file);
			print $file;
		}
		$contents = ob_get_contents();
		ob_end_clean();
		return $contents;
	}
}